/* 
 * The MIT License
 *
 * Copyright 2014 Venkat Ram Akkineni.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.netbeans.modules.tfsnb.core;

import com.microsoft.tfs.core.clients.framework.configuration.entities.ProjectCollectionEntity;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Venkat Ram Akkineni
 */
public class AddExistingTeamProjectPanel extends javax.swing.JPanel {

    private final AddExistingTeamProjectController controller;
    private final ConnectionSettings currentConnectionSettings;
    
    /**
     * Creates new form MapProjectCollections
     */
    public AddExistingTeamProjectPanel(final ConnectionSettings currentConnectionSettings) {
        controller = new AddExistingTeamProjectController(currentConnectionSettings);
        this.currentConnectionSettings = currentConnectionSettings;
        initComponents();
        
        teamProjectCollectionsList.setSelectedIndex(0);
        //TODO: Retrieve the Configuration Server preference and select it in the drop down. If the preference doesn't exist, select the first one.
        populateProjectCollectionList();
        populateProjectList();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        serversComboBox = new javax.swing.JComboBox();
        serversComboBox.addActionListener(controller);
        teamProjectsSplitPane = new javax.swing.JSplitPane();
        teamProjectsSplitPane.setResizeWeight(.5d);
        teamProjectCollectionsScrollPane = new javax.swing.JScrollPane();
        teamProjectCollectionsList = new javax.swing.JList();
        teamProjectScrollPane = new javax.swing.JScrollPane();
        teamProjectsList = new javax.swing.JList();
        progressBar = new javax.swing.JProgressBar();

        serversComboBox.setModel(new DefaultComboBoxModel(controller.getServerNames()));
        serversComboBox.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        teamProjectsSplitPane.setDividerLocation(340);
        teamProjectsSplitPane.setDividerSize(5);

        teamProjectCollectionsList.setModel(new DefaultListModel<ProjectCollectionEntityView>());
        teamProjectCollectionsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        teamProjectCollectionsList.addListSelectionListener(projectCollectionListSelectionListener);
        teamProjectCollectionsScrollPane.setViewportView(teamProjectCollectionsList);

        teamProjectsSplitPane.setLeftComponent(teamProjectCollectionsScrollPane);

        teamProjectsList.setModel(new DefaultListModel<TeamProjectEntityView>());
        teamProjectsList.addListSelectionListener(teamProjectListSelectionListener);
        teamProjectScrollPane.setViewportView(teamProjectsList);

        teamProjectsSplitPane.setRightComponent(teamProjectScrollPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(teamProjectsSplitPane, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(serversComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 687, Short.MAX_VALUE)
                    .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(serversComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(teamProjectsSplitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public void okButtonPressed() {
        
    }
    
    private void populateProjectCollectionList() {
        ProjectCollectionEntityView[] pces = controller.getProjectCollectionEntityViews();
        DefaultListModel listModel = (DefaultListModel) teamProjectCollectionsList.getModel();
        for(ProjectCollectionEntityView pce: pces) {
            listModel.addElement(pce);
        }
    }
    
    private void populateProjectList() {
        TeamProjectEntityView[] tpes = controller.getTeamProjectEntityViews();
        DefaultListModel listModel = (DefaultListModel) teamProjectsList.getModel();
        for(TeamProjectEntityView tpev: tpes) {
            listModel.addElement(tpev);
        }       
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JComboBox serversComboBox;
    private javax.swing.JList teamProjectCollectionsList;
    private javax.swing.JScrollPane teamProjectCollectionsScrollPane;
    private javax.swing.JScrollPane teamProjectScrollPane;
    private javax.swing.JList teamProjectsList;
    private javax.swing.JSplitPane teamProjectsSplitPane;
    // End of variables declaration//GEN-END:variables

    private ListSelectionListener projectCollectionListSelectionListener = new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {            
            controller.updateTeamProjectCollectionSelections(teamProjectCollectionsList.getSelectedIndex());
        }

    };

    private ListSelectionListener teamProjectListSelectionListener = new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            controller.updateTeamProjectSelections(teamProjectsList.getSelectedIndices());
        }

    };
    
}
